package tec.line;


import java.awt.Color;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawKLing {
	private JFrame frmPaint = new JFrame("Paint Demo");
	private JPanel pnlPaint = new JPanel();

	public DrawKLing () {
		
	}
	
	void draw(final PanelDomain pd){
		MouseAdapter ma = new MouseAdapter() {
			@Override
			public void mousePressed (MouseEvent me) {
				
				drawTheLine(pd,pnlPaint);
				
//				 Panel panel = new Panel();
//				 panel.setBackground(Color.RED);
//			     panel.setBounds(245, 355, 10, -55);
//			     pnlPaint.add(panel);
			     
//			     Panel panell = new Panel();
//			     panell.setBackground(Color.BLACK);
//			     panell.setBounds(27, 13, 2, 60);
//			     pnlPaint.add(panell);
//			     
//			     
//			     Panel panel1 = new Panel();
//			     panel1.setBackground(Color.GREEN);
//			     panel1.setBounds(78, 25, 30, 40);
//			     pnlPaint.add(panel1);
			}

			private void drawTheLine(PanelDomain pd, JPanel pnlPaint) {
				pnlPaint.removeAll();
				pnlPaint.repaint();
				pd.setShow_datas(pd.getDatas());
				pd.calculate(pd);
				List<IndexData> show_datas = pd.getShow_datas();
				List<IndexData> vertical_Line = pd.getVertical_Line();
				int max = 0;
				
				Panel x_line = new Panel();
				x_line.setBackground(Color.BLUE);
				 x_line.setBounds(0,600,800,1);
			     pnlPaint.add(x_line);
			     
			     Panel y_line = new Panel();
			     y_line.setBackground(Color.BLUE);
			     y_line.setBounds(0,0,1,600);
			     pnlPaint.add(y_line);
				
				for (int i = 0; i < vertical_Line.size(); i++) {
					Panel line = new Panel();
				     line.setBackground(Color.GRAY);
				     System.out.println("i:" + vertical_Line.get(i).getP_open().getX());
				     line.setBounds(vertical_Line.get(i).getP_open().getX() -1, 
				    		 0,
				    		 1, 
				    		 600);
				     pnlPaint.add(line);
				}
				
				
				
				for (int i = 0; i < show_datas.size(); i++) {
					 Panel panel = new Panel();
					 panel.setBackground(Color.RED);
					 if (show_datas.get(i).getIndex_close() < show_datas.get(i).getIndex_open()) {
						 panel.setBackground(Color.GREEN);
						
					}
					 max = show_datas.get(i).getP_open().getY() > show_datas.get(i).getP_close().getY()?show_datas.get(i).getP_open().getY():show_datas.get(i).getP_close().getY(); 
				     panel.setBounds(show_datas.get(i).getP_open().getX(), 
				    		 pd.getPx_hight() - max ,
				    		 pd.getReal_width(), 
				    		 Math.abs(show_datas.get(i).getP_open().getY() - show_datas.get(i).getP_close().getY()) );
				     pnlPaint.add(panel);
				     
				     Panel line = new Panel();
				     line.setBackground(Color.black);
				     line.setBounds(show_datas.get(i).getP_max().getX(), 
				    		 pd.getPx_hight() - show_datas.get(i).getP_max().getY(),
				    		 2, 
				    		 Math.abs(show_datas.get(i).getP_max().getY() - show_datas.get(i).getP_min().getY()));
				     pnlPaint.add(line);
				}
				
			}
		};

		pnlPaint.addMouseListener(ma);
		pnlPaint.addMouseMotionListener(ma);

		//初始化主窗口
		frmPaint.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmPaint.setSize(800, 700);
		frmPaint.setLocationRelativeTo(null);
		frmPaint.setContentPane(pnlPaint);
		
		frmPaint.setVisible(true);
		
	}

	public static void main (String args[]) {
		
		PanelDomainProvider pdp = new PanelDomainProvider();
		PanelDomain pd = pdp.createPanelDomain();
		
		
		new DrawKLing().draw(pd);;
	}
}